//
//  MK_TitleContentVC.m
//  miku直播
//
//  Created by 杨尚达 on 2017/7/23.
//  Copyright © 2017年 杨尚达. All rights reserved.
//

#import "MK_TitleContentVC.h"
#import "MK_Title_Cell.h"

#define MK_TitleVC_Cell @"MK_TitleVC_Cell"
#define MK_TitleVC_TitleMarkHeight 6

@interface MK_TitleContentVC () <UICollectionViewDataSource,UICollectionViewDelegate>

///标题视图
@property(nonatomic,weak)UICollectionView* titleV;

///标题模型数组
@property(nonatomic,strong)NSMutableArray<MK_TitleModel*>* modelArr;

///当前选中的标题index
@property(nonatomic,assign)NSInteger selectedindex;

///标题下滑块
@property(nonatomic,weak)UIView* markV;

///frame大小
@property(nonatomic,assign)CGRect selfFrame;

@end

@implementation MK_TitleContentVC


+(instancetype)MK_TitleContentVCWithFrame:(CGRect)frame andTitleArr:(NSArray<NSString*>*)titleArr andDelegate:(id<UICollectionViewDelegate,UICollectionViewDataSource>) delegate  andTitleWidth:(CGFloat)width{
    MK_TitleContentVC* tcVC = [MK_TitleContentVC new];
    tcVC.titleArr = titleArr;
    tcVC.contentDelegate = delegate;
    tcVC.titleWidth = width;
    tcVC.view.frame = frame;
    return tcVC;
}

-(void)loadView{
    [super loadView];
    [self initData];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self setUpUI];
}

///初始化界面
-(void)setUpUI{
    self.view.backgroundColor = [UIColor blackColor];
    
    UICollectionViewFlowLayout* titleLayout = [UICollectionViewFlowLayout new];
    UICollectionViewFlowLayout* contentLayout = [UICollectionViewFlowLayout new];
    
    titleLayout.itemSize = CGSizeMake(_titleWidth, _titleHeight);
    titleLayout.minimumLineSpacing = 0;
    titleLayout.minimumInteritemSpacing = 0;
    titleLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    contentLayout.itemSize = CGSizeMake(self.view.bounds.size.width, self.view.bounds.size.height - self.titleHeight-1);
    contentLayout.minimumInteritemSpacing = 0;
    contentLayout.minimumLineSpacing = 0;
    contentLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    
    UICollectionView* titleV = [[UICollectionView alloc]initWithFrame:CGRectMake(0,0 , self.view.bounds.size.width, self.titleHeight) collectionViewLayout:titleLayout];
    _titleV = titleV;
    titleV.delegate = self;
    titleV.dataSource = self;
    [titleV registerNib:[UINib nibWithNibName:@"MK_Title_Cell" bundle:nil] forCellWithReuseIdentifier:MK_TitleVC_Cell];
    titleV.backgroundColor = self.titleBackColor;
    [self.view addSubview:titleV];
    [self getModelArr];
    
    UIView* markV = [UIView new];
    _markV = markV;
    markV.frame = CGRectMake(0, self.titleHeight-MK_TitleVC_TitleMarkHeight, self.titleWidth, MK_TitleVC_TitleMarkHeight);
    markV.backgroundColor = self.titleSelectedColer;
    [titleV addSubview:markV];
    
    UICollectionView* contentV = [[UICollectionView alloc]initWithFrame:CGRectMake(0, self.titleHeight+1, self.view.bounds.size.width, self.view.bounds.size.height - self.titleHeight-1) collectionViewLayout:contentLayout];
    _contentV = contentV;
    contentV.delegate = self.contentDelegate;
    contentV.dataSource = self.contentDelegate;
    contentV.backgroundColor = _contentBackColor;
    contentV.pagingEnabled = YES;
    [contentV addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew context:nil];
    if (_cellNibDic != nil) {
        [_cellNibDic enumerateKeysAndObjectsUsingBlock:^(NSString * _Nonnull key, UINib * _Nonnull obj, BOOL * _Nonnull stop) {
            [_contentV registerNib:obj forCellWithReuseIdentifier:key];
        }];
    }
    if (_cellCalssDic != nil) {
        [_cellCalssDic enumerateKeysAndObjectsUsingBlock:^(NSString * _Nonnull key, Class  _Nonnull obj, BOOL * _Nonnull stop) {
            [_contentV registerClass:obj forCellWithReuseIdentifier:key];
        }];
    }
    [self.view addSubview:contentV];
}


#pragma mark 代理方法
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.titleArr.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    MK_Title_Cell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:MK_TitleVC_Cell forIndexPath:indexPath];
    cell.model = _modelArr[indexPath.item];
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    CGPoint movePoint = CGPointMake(self.view.bounds.size.width*indexPath.item, 0);
    [_contentV setContentOffset:movePoint animated:YES];
}

#pragma mark 监听内容视图的滚动方法
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
    
    
    
    NSValue* pointValue = change[NSKeyValueChangeNewKey];
    CGFloat offX = [pointValue CGPointValue].x;
    NSInteger item = (NSInteger)(offX/(self.view.bounds.size.width))+0.5;
    self.selectedindex = item;
    if (self.view.bounds.size.width != 0) {
        _markV.frame = CGRectMake(offX*(self.titleWidth/self.view.bounds.size.width), self.titleHeight-MK_TitleVC_TitleMarkHeight,  self.titleWidth, MK_TitleVC_TitleMarkHeight);
    }
}

#pragma mark 数据方法
///数据初始化
-(void)initData{
    if (_titleHeight == 0) {
        _titleHeight = 30;
    }
    if(_titleWidth == 0){
        _titleWidth = 50;
    }
    if (_titleBackColor == nil) {
        _titleBackColor = [UIColor whiteColor];
    }
    if (_contentBackColor == nil) {
        _contentBackColor = [UIColor groupTableViewBackgroundColor];
    }
    if (_titleColer == nil) {
        _titleColer = [UIColor blackColor];
    }
    if (_titleSelectedColer == nil) {
        _titleSelectedColer = [UIColor orangeColor];
    }
}

-(void)dealloc{
    [_contentV removeObserver:self forKeyPath:@"contentOffset"];
}

///得到模型数组
-(void)getModelArr{
    NSMutableArray<MK_TitleModel*>* arr = [NSMutableArray new];
    [_titleArr enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        MK_TitleModel* model = [MK_TitleModel MK_TitleModelWithTitle:obj andNormalColor:_titleColer andSelectedColor:_titleSelectedColer];
        [arr addObject:model];
    }];
    arr.firstObject.isSelected = YES;
    _modelArr = arr;
}

-(void)setSelectedindex:(NSInteger)selectedindex{
    if (selectedindex>=_modelArr.count) {
        return;
    }
    if (_selectedindex != selectedindex) {
        _modelArr[_selectedindex].isSelected = NO;
        _modelArr[selectedindex].isSelected = YES;
        _selectedindex = selectedindex;
        [self.titleV reloadData];
    }
}

-(void)reloadContentData{
    [_titleV reloadData];
    [_contentV reloadData];
}

@end
